package com.wlps.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wlps.bo.QueryComposeBo;
import com.wlps.config.RedisService;
import com.wlps.entity.Order;
import com.wlps.entity.OrderRecord;
import com.wlps.entity.SysUser;
import com.wlps.result.ResultPageVo;
import com.wlps.result.ResultUtils;
import com.wlps.result.ResultVo;
import com.wlps.service.ICommodityService;
import com.wlps.service.IOrderRecordService;
import com.wlps.service.IOrderService;
import com.wlps.service.ISysUserService;
import com.wlps.service.function.DispatchingMangementCenter;
import com.wlps.utils.PageUtils;
import com.wlps.vo.OperatorDetailInfoVo;
import com.wlps.vo.OperatorNumberVo;
import io.swagger.annotations.Api;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

@Api(tags="操作员管理")
@RestController
@RequestMapping("/api/operator/")
@Slf4j
public class OperatorController {
    @Resource
    private ISysUserService sysUserService;

    @Resource
    IOrderRecordService orderRecordService;

    @GetMapping("query_all_sysUser")
    @ApiOperation("查询所有所有用户使用")
    public ResultVo querySysUser(){
        Map<String, OperatorNumberVo> stringOperatorNumberVoMap = orderRecordService.queryAllOperator();

        return ResultUtils.success("查询成功",new ArrayList<>(stringOperatorNumberVoMap.values()));
    }
    @GetMapping("query_all_sysUser/{require}")

    @ApiOperation("根据需求查询用户列表")
    public ResultVo<OperatorNumberVo> querySysUserAccordingRequire( @PathVariable @ApiParam("查询信息") String require){
        boolean digit = Character.isDigit(require.charAt(require.length() - 1));
        Map<String, OperatorNumberVo> stringOperatorNumberVoMap = orderRecordService.queryAllOperator();
        OperatorNumberVo operatorNumberVo=null;
        if(digit){
            OrderRecord byId = orderRecordService.getById(digit);

             operatorNumberVo = stringOperatorNumberVoMap.get(byId.getOperator());
        }
        else
        {

             operatorNumberVo = stringOperatorNumberVoMap.get(require);
        }
        return ResultUtils.success("查询成功",operatorNumberVo);
    }
    @GetMapping("query_sysUser_detail_info/{id}")
    @ApiOperation("根据管理员id查询详细信息")
    public ResultPageVo<OperatorDetailInfoVo> querySysUserDetailInfo(@PathVariable @ApiParam("用户id")long id){
        List<OperatorDetailInfoVo> orderRecords = sysUserService.operatorDetailinfoById(id);
        ResultPageVo<OperatorDetailInfoVo> pageInfo = PageUtils.getPageInfo(orderRecords);
        log.info(orderRecords.toString());
        return pageInfo;
    }

    @ApiOperation("根据id查询商品名")
    @GetMapping("get_commodity_by_id")
    public ResultVo<List<String>>queryCommodityNameById(@PathVariable long id){
        List<String> stringList = sysUserService.queryCommodityByOrderId(id);
        return ResultUtils.success("查询成功",stringList);
    }
    @PostMapping("query_sysUser_according_require/{id}")
    @ApiOperation("根据管理员需求查询信息查询详细信息")
    public ResultVo<OperatorDetailInfoVo>querySysUserAccordingRequire(@RequestBody @ApiParam("用户查询信息") QueryComposeBo querySysUserBo, @PathVariable long id){
        List<OperatorDetailInfoVo> orderRecords = sysUserService.operatorDetailinfoById(id);
        List<OperatorDetailInfoVo> collect = orderRecords.stream()
                .filter(orderRecord->{
                    if(querySysUserBo.getQueryType().equals("订单ID")){
                        return orderRecord.getId()==Long.parseLong(querySysUserBo.getQueryContain());
                    }
                    else
                        return orderRecord.getOrderStatus()==Integer.parseInt(querySysUserBo.getQueryContain());
                        }
                ).collect(Collectors.toList());
        return ResultUtils.success("查询成功",collect);
    }

}
